package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.PrinterSerialNumber;
import com.ruoyi.nongjt.service.PrinterSerialNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 打印机序列号管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/nongjt/printerSerialNumber")
public class PrinterSerialNumberController extends BaseController
{
    @Autowired
    private PrinterSerialNumberService printerSerialNumberService;

    /**
     * 查询打印机序列号管理列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:list')")
    @GetMapping("/list")
    public TableDataInfo list(PrinterSerialNumber printerSerialNumber)
    {
        startPage();
        List<PrinterSerialNumber> list = printerSerialNumberService.selectPrinterSerialNumberList(printerSerialNumber);
        return getDataTable(list);
    }

    /**
     * 导出打印机序列号管理列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:export')")
    @Log(title = "打印机序列号管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PrinterSerialNumber printerSerialNumber)
    {
        List<PrinterSerialNumber> list = printerSerialNumberService.selectPrinterSerialNumberList(printerSerialNumber);
        ExcelUtil<PrinterSerialNumber> util = new ExcelUtil<PrinterSerialNumber>(PrinterSerialNumber.class);
        util.exportExcel(response, list, "打印机序列号管理数据");
    }

    /**
     * 获取打印机序列号管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(printerSerialNumberService.selectPrinterSerialNumberById(id));
    }

    /**
     * 新增打印机序列号管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:add')")
    @Log(title = "打印机序列号管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrinterSerialNumber printerSerialNumber)
    {
        if ("1".equals(printerSerialNumberService.checkSerialNumberUnique(printerSerialNumber)))
        {
            return error("新增打印机序列号'" + printerSerialNumber.getSerialNumber() + "'失败，序列号已存在");
        }
        printerSerialNumber.setCreateBy(getUsername());
        return toAjax(printerSerialNumberService.insertPrinterSerialNumber(printerSerialNumber));
    }

    /**
     * 修改打印机序列号管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:edit')")
    @Log(title = "打印机序列号管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrinterSerialNumber printerSerialNumber)
    {
        if ("1".equals(printerSerialNumberService.checkSerialNumberUnique(printerSerialNumber)))
        {
            return error("修改打印机序列号'" + printerSerialNumber.getSerialNumber() + "'失败，序列号已存在");
        }
        printerSerialNumber.setUpdateBy(getUsername());
        return toAjax(printerSerialNumberService.updatePrinterSerialNumber(printerSerialNumber));
    }

    /**
     * 删除打印机序列号管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:remove')")
    @Log(title = "打印机序列号管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(printerSerialNumberService.deletePrinterSerialNumberByIds(ids));
    }

    /**
     * 获取打印机序列号管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:query')")
    @GetMapping(value = "/checkSerialNumberUnique")
    public AjaxResult checkSerialNumberUnique(PrinterSerialNumber printerSerialNumber)
    {
        return success(printerSerialNumberService.checkSerialNumberUnique(printerSerialNumber));
    }

    /**
     * 导入打印机序列号数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:import')")
    @Log(title = "打印机序列号管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PrinterSerialNumber> util = new ExcelUtil<PrinterSerialNumber>(PrinterSerialNumber.class);
        List<PrinterSerialNumber> printerSerialNumberList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = printerSerialNumberService.importPrinterSerialNumber(printerSerialNumberList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerSerialNumber:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<PrinterSerialNumber> util = new ExcelUtil<PrinterSerialNumber>(PrinterSerialNumber.class);
        util.importTemplateExcel(response, "打印机序列号数据");
    }
}
